package com.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.jinlejia.oldmodule.R;

/**
 * Created by 王进 on 2019/7/1.
 */

public class JLJCommonDialog extends AlertDialog implements View.OnClickListener {
    private String title,content,cancel,confirm;
    public JLJCommonDialog(@NonNull Context context) {
        this(context,null,null);
    }

    public JLJCommonDialog(@NonNull Context context, String title,String content) {
        this (context,title,content,null,null);
    }

    public JLJCommonDialog(Context context,String title,String content,String confirm,String cancel) {
        super(context);
        this.title = title;
        this.content = content;
        this.cancel = cancel;
        this.confirm = confirm;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = this.getWindow();
        // *** 主要就是在这里实现这种效果的.
        // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
        this.setCanceledOnTouchOutside(false);
        window.setContentView(R.layout.jlj_dialog_common);
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawable(new ColorDrawable());
        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        //Title
        TextView titleTxt = (TextView) window.findViewById(R.id.title);
        titleTxt.setText(title);
        //Content
        TextView contentTxt = (TextView) window.findViewById(R.id.content);
        contentTxt.setText( content );
        //取消
        TextView cancleTxt = (TextView) window.findViewById(R.id.id_cancel);
        if(!TextUtils.isEmpty(cancel))
            cancleTxt.setText(cancel);
        //确定
        TextView confirmTxt = (TextView) window.findViewById(R.id.id_confirm);
        if( !TextUtils.isEmpty( confirm ) )
            confirmTxt.setText(confirm);


        cancleTxt.setOnClickListener(this);
        confirmTxt.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.id_cancel) {
            dismiss();
            if (null != cancelListener) {
                cancelListener.cancel();
            }

        } else if (i == R.id.id_confirm) {//确定
            dismiss();
            if (null != confirmListener)
                confirmListener.confirm();

        } else {
        }
    }

    public interface OnConfirmListener {
        void confirm();
    }

    public interface OnCancelListener{
        void cancel();
    }

    private OnConfirmListener confirmListener;

    public void setConfirmListener(OnConfirmListener listener) {
        this.confirmListener = listener;
    }

    private OnCancelListener cancelListener;
    public void setCancelListener(OnCancelListener listener){
        this.cancelListener = listener;
    }
}
